/**
 * NFT 相关 API
 */

import request from '../request'

/**
 * NFT 信息接口
 */
export interface NFTInfo {
  tokenId: string
  owner: string
  tokenURI: string
  metadata?: {
    name: string
    description: string
    image: string
    attributes?: Array<{
      trait_type: string
      value: string | number
    }>
  }
  mintedAt: string
}

/**
 * 铸造 NFT 请求参数
 */
export interface MintNFTRequest {
  to: string
  tokenURI: string
  metadata?: {
    name: string
    description: string
    image: string
    attributes?: Array<{
      trait_type: string
      value: string | number
    }>
  }
}

/**
 * 转移 NFT 请求参数
 */
export interface TransferNFTRequest {
  tokenId: string
  from: string
  to: string
}

/**
 * NFT API
 */
export const nftApi = {
  /**
   * 铸造 NFT
   */
  mintNFT: (data: MintNFTRequest) =>
    request.post<{ tokenId: string; txHash: string }>('/blockchain/nft/mint', data),

  /**
   * 批量铸造 NFT
   */
  batchMintNFT: (data: { recipients: MintNFTRequest[] }) =>
    request.post<{ tokenIds: string[]; txHash: string }>('/blockchain/nft/batch-mint', data),

  /**
   * 转移 NFT
   */
  transferNFT: (data: TransferNFTRequest) =>
    request.post<{ txHash: string }>('/blockchain/nft/transfer', data),

  /**
   * 获取 NFT 信息
   */
  getNFTInfo: (tokenId: string) =>
    request.get<NFTInfo>(`/blockchain/nft/${tokenId}`),

  /**
   * 获取用户的 NFT 列表
   */
  getUserNFTs: (address: string) =>
    request.get<NFTInfo[]>(`/blockchain/nft/user/${address}`),

  /**
   * 获取家族的 NFT 列表
   */
  getFamilyNFTs: (familyId: string) =>
    request.get<NFTInfo[]>(`/blockchain/nft/family/${familyId}`),

  /**
   * 上传 NFT 元数据到 IPFS
   */
  uploadMetadata: (data: {
    name: string
    description: string
    image: File | string
    attributes?: Array<{
      trait_type: string
      value: string | number
    }>
  }) =>
    request.post<{ ipfsHash: string; tokenURI: string }>('/blockchain/nft/upload-metadata', data, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }),

  /**
   * 销毁 NFT
   */
  burnNFT: (tokenId: string) =>
    request.post<{ txHash: string }>(`/blockchain/nft/${tokenId}/burn`),

  /**
   * 获取 NFT 总供应量
   */
  getTotalSupply: () =>
    request.get<{ totalSupply: number }>('/blockchain/nft/total-supply'),

  /**
   * 设置 NFT 批准
   */
  approveNFT: (data: { tokenId: string; approved: string }) =>
    request.post<{ txHash: string }>('/blockchain/nft/approve', data),

  /**
   * 检查 NFT 所有权
   */
  ownerOf: (tokenId: string) =>
    request.get<{ owner: string }>(`/blockchain/nft/${tokenId}/owner`),
}

export default nftApi

